/**
 * 渲染系统模块
 * 负责游戏中所有渲染相关的功能
 */

class GameRenderer {
    constructor(ctx) {
        this.ctx = ctx;
    }

    // 渲染简单草地背景
    renderSimpleGrass(terrainDetails) {
        const grassGradient = this.ctx.createLinearGradient(0, 0, window.innerWidth, window.innerHeight);
        grassGradient.addColorStop(0, '#2d4a2d');
        grassGradient.addColorStop(0.3, '#3d5a3d');
        grassGradient.addColorStop(0.7, '#2d4a2d');
        grassGradient.addColorStop(1, '#1d3a1d');
        
        this.ctx.fillStyle = grassGradient;
        this.ctx.fillRect(0, 0, window.innerWidth, window.innerHeight);
        
        if (terrainDetails) {
            for (const point of terrainDetails.noisePoints) {
                this.ctx.globalAlpha = point.opacity;
                this.ctx.fillStyle = point.color;
                this.ctx.fillRect(point.x, point.y, point.size, point.size);
            }
            this.ctx.globalAlpha = 1;
        }
    }

    // 渲染建筑血量条
    renderBuildingHealthBar(building) {
        const x = building.position.x + building.size.x / 2;
        const y = building.position.y - 10;
        const width = Math.min(building.size.x * 0.8, 40);
        const height = 4;
        
        this.ctx.fillStyle = '#000000';
        this.ctx.fillRect(x - width/2 - 1, y - 1, width + 2, height + 2);
        
        this.ctx.fillStyle = '#333333';
        this.ctx.fillRect(x - width/2, y, width, height);
        
        const healthRatio = Math.max(0, Math.min(1, building.health / building.maxHealth));
        const healthWidth = width * healthRatio;
        
        let healthColor;
        if (healthRatio > 0.7) {
            healthColor = '#8b4513';
        } else if (healthRatio > 0.4) {
            healthColor = '#ff8800';
        } else {
            healthColor = '#ff0000';
        }
        
        if (healthWidth > 0) {
            this.ctx.fillStyle = healthColor;
            this.ctx.fillRect(x - width/2, y, healthWidth, height);
        }
        
        this.ctx.strokeStyle = '#ffffff';
        this.ctx.lineWidth = 1;
        this.ctx.strokeRect(x - width/2, y, width, height);
    }

    // 渲染简单建筑
    renderSimpleBuilding(building) {
        const pos = building.position;
        const size = building.size;
        const type = building.type;
        
        switch (type) {
            case 'steel':
                this.renderSimpleSteelBuilding(pos.x, pos.y, size.x, size.y);
                break;
            default:
                this.ctx.fillStyle = '#666666';
                this.ctx.fillRect(pos.x, pos.y, size.x, size.y);
                break;
        }
    }

    // 渲染钢制建筑
    renderSimpleSteelBuilding(x, y, w, h) {
        const steelGradient = this.ctx.createLinearGradient(x, y, x + w, y + h);
        steelGradient.addColorStop(0, '#888888');
        steelGradient.addColorStop(0.5, '#666666');
        steelGradient.addColorStop(1, '#333333');
        
        this.ctx.fillStyle = steelGradient;
        this.ctx.fillRect(x, y, w, h);
        
        this.ctx.strokeStyle = '#aaaaaa';
        this.ctx.lineWidth = 1;
        this.ctx.strokeRect(x, y, w, h);
    }

    // 渲染穿甲弹
    renderAPFSDSBullet(bullet) {
        const x = bullet.position.x;
        const y = bullet.position.y;
        const angle = bullet.angle;
        
        this.ctx.save();
        this.ctx.translate(x, y);
        this.ctx.rotate(angle);
        
        const bulletColors = bullet.owner === 'player' ? {
            body: '#b8860b',
            tip: '#2c2c2c',
            highlight: '#daa520'
        } : {
            body: '#8b6969',
            tip: '#2c2c2c',
            highlight: '#cd853f'
        };
        
        const bodyLength = 12;
        const bodyRadius = 1.5;
        
        const bodyGradient = this.ctx.createLinearGradient(-bodyLength/2, 0, bodyLength/2, 0);
        bodyGradient.addColorStop(0, bulletColors.body);
        bodyGradient.addColorStop(1, bulletColors.highlight);
        
        this.ctx.fillStyle = bodyGradient;
        this.ctx.fillRect(-bodyLength/2, -bodyRadius, bodyLength, bodyRadius * 2);
        
        const tipLength = 4;
        this.ctx.fillStyle = bulletColors.tip;
        this.ctx.beginPath();
        this.ctx.moveTo(bodyLength/2 + tipLength, 0);
        this.ctx.lineTo(bodyLength/2, -bodyRadius);
        this.ctx.lineTo(bodyLength/2, bodyRadius);
        this.ctx.closePath();
        this.ctx.fill();
        
        this.ctx.restore();
    }

    // 渲染导弹
    renderMissile(missile) {
        const x = missile.position.x;
        const y = missile.position.y;
        const angle = missile.angle;
        
        this.ctx.save();
        this.ctx.translate(x, y);
        this.ctx.rotate(angle);
        
        // 导弹主体
        const bodyLength = 20;
        const bodyRadius = 3;
        
        // 导弹渐变色（橙红色）
        const bodyGradient = this.ctx.createLinearGradient(-bodyLength/2, 0, bodyLength/2, 0);
        bodyGradient.addColorStop(0, '#ff3300');
        bodyGradient.addColorStop(0.3, '#ff6600');
        bodyGradient.addColorStop(0.7, '#ff9900');
        bodyGradient.addColorStop(1, '#ffcc00');
        
        this.ctx.fillStyle = bodyGradient;
        this.ctx.fillRect(-bodyLength/2, -bodyRadius, bodyLength, bodyRadius * 2);
        
        // 导弹头部（尖锐的弹头）
        const tipLength = 6;
        this.ctx.fillStyle = '#ff0000';
        this.ctx.beginPath();
        this.ctx.moveTo(bodyLength/2 + tipLength, 0);
        this.ctx.lineTo(bodyLength/2, -bodyRadius);
        this.ctx.lineTo(bodyLength/2, bodyRadius);
        this.ctx.closePath();
        this.ctx.fill();
        
        // 导弹尾翼
        this.ctx.fillStyle = '#666666';
        this.ctx.fillRect(-bodyLength/2 - 3, -bodyRadius - 2, 8, 1);
        this.ctx.fillRect(-bodyLength/2 - 3, bodyRadius + 1, 8, 1);
        
        // 发光效果
        this.ctx.shadowColor = missile.color;
        this.ctx.shadowBlur = 8;
        this.ctx.strokeStyle = missile.color;
        this.ctx.lineWidth = 1;
        this.ctx.strokeRect(-bodyLength/2, -bodyRadius, bodyLength, bodyRadius * 2);
        
        this.ctx.restore();
    }

    // 渲染简单坦克
    renderSimpleTank(x, y, angle, type, tank = null) {
        this.ctx.save();
        this.ctx.translate(x, y);
        this.ctx.rotate(angle);
        
        const colors = type === 'player' ? {
            body: '#4a7c59',
            turret: '#2d5a3d', 
            barrel: '#95a8d0',
            track: '#2a2a2a',
            highlight: '#6ba567'
        } : {
            body: '#7c4a5a',
            turret: '#5a2d3d',
            barrel: '#d095a8', 
            track: '#2a2a2a',
            highlight: '#a56777'
        };
        
        // 履带
        this.ctx.fillStyle = colors.track;
        this.ctx.fillRect(-12, -18, 6, 36);
        this.ctx.fillRect(6, -18, 6, 36);
        
        // 车体
        this.ctx.fillStyle = colors.body;
        this.ctx.fillRect(-6, -16, 12, 32);
        
        // 炮塔
        this.renderTShapeTurret(colors, 0, 2);
        
        // 主炮（应用后坐力）
        const recoilOffset = tank ? tank.recoilOffset : 0;
        this.ctx.fillStyle = colors.barrel;
        this.ctx.fillRect(-1.5, 2 + recoilOffset, 3, 20 - recoilOffset);
        
        this.ctx.restore();
    }

    // 渲染T形炮塔
    renderTShapeTurret(colors, centerX, centerY) {
        const turretWidth = 14;
        const turretHeight = 6;
        const baseWidth = 8;
        const baseHeight = 10;
        
        // 横向部分
        const horizontalGradient = this.ctx.createLinearGradient(
            centerX, centerY - turretHeight/2, 
            centerX, centerY + turretHeight/2
        );
        horizontalGradient.addColorStop(0, colors.highlight);
        horizontalGradient.addColorStop(0.5, colors.turret);
        horizontalGradient.addColorStop(1, colors.body);
        
        this.ctx.fillStyle = horizontalGradient;
        this.ctx.fillRect(
            centerX - turretWidth/2, centerY - turretHeight/2,
            turretWidth, turretHeight
        );
        
        // 纵向部分
        const verticalGradient = this.ctx.createLinearGradient(
            centerX - baseWidth/2, centerY,
            centerX + baseWidth/2, centerY
        );
        verticalGradient.addColorStop(0, colors.body);
        verticalGradient.addColorStop(0.5, colors.highlight);
        verticalGradient.addColorStop(1, colors.body);
        
        this.ctx.fillStyle = verticalGradient;
        this.ctx.fillRect(
            centerX - baseWidth/2, centerY - 1,
            baseWidth, baseHeight
        );
        
        // 中央螺栓
        this.ctx.fillStyle = colors.highlight;
        this.ctx.beginPath();
        this.ctx.arc(centerX, centerY, 1.5, 0, Math.PI * 2);
        this.ctx.fill();
    }

    // 渲染血量条
    renderHealthBar(x, y, currentHealth, maxHealth, width = 40, height = 6) {
        this.ctx.save();
        this.ctx.shadowBlur = 0;
        
        this.ctx.fillStyle = '#000000';
        this.ctx.fillRect(x - width/2 - 1, y - 31, width + 2, height + 2);
        
        this.ctx.fillStyle = '#333333';
        this.ctx.fillRect(x - width/2, y - 30, width, height);
        
        const healthRatio = Math.max(0, Math.min(1, currentHealth / maxHealth));
        const healthWidth = width * healthRatio;
        
        let healthColor;
        if (healthRatio > 0.7) {
            healthColor = '#00ff00';
        } else if (healthRatio > 0.4) {
            healthColor = '#ffff00';
        } else if (healthRatio > 0.2) {
            healthColor = '#ff8800';
        } else {
            healthColor = '#ff0000';
        }
        
        if (healthWidth > 0) {
            this.ctx.fillStyle = healthColor;
            this.ctx.fillRect(x - width/2, y - 30, healthWidth, height);
        }
        
        this.ctx.strokeStyle = '#ffffff';
        this.ctx.lineWidth = 1;
        this.ctx.strokeRect(x - width/2, y - 30, width, height);
        
        this.ctx.restore();
    }

    // 渲染游戏UI
    renderUI(fps, particleCount, enemyCount, bulletCount) {
        this.ctx.save();
        this.ctx.shadowBlur = 0; // 清除阴影效果
        
        this.ctx.fillStyle = '#00ff88';
        this.ctx.font = '16px Courier New';
        this.ctx.fillText(`FPS: ${fps}`, 20, window.innerHeight - 80);
        this.ctx.fillText(`粒子: ${particleCount}`, 20, window.innerHeight - 60);
        this.ctx.fillText(`敌人: ${enemyCount}`, 20, window.innerHeight - 40);
        this.ctx.fillText(`子弹: ${bulletCount}`, 20, window.innerHeight - 20);
        
        this.ctx.restore();
    }
}

// 导出到window对象
window.GameRenderer = GameRenderer; 